package com.itheima.tliasbackend.controller;

import com.itheima.tliasbackend.annotation.SysLog;
import com.itheima.tliasbackend.pojo.Emp;
import com.itheima.tliasbackend.service.EmpService;
import com.itheima.tliasbackend.utils.PageBean;
import com.itheima.tliasbackend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

// 员工管理控制器
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result getList(
            String name,
            Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);
        return Result.success(pageBean);
    }

    @GetMapping
    public Result testErr() {
        return Result.success();
    }

    @SysLog
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        empService.removeByIds(idList);
        return Result.success();
    }

    @SysLog
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        empService.save(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @SysLog
    @PutMapping
    public Result updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return Result.success();
    }
}